package com.cj.mvvmlib.ext.view

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.util.LayoutDirection
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.text.TextUtilsCompat
import com.cj.mvvmlib.R
import com.cj.mvvmlib.ext.util.dp2px
import java.util.*

class TitleBarView @JvmOverloads constructor(context: Context,
    private val attrs: AttributeSet? = null,
    defStyle: Int = 0) : LinearLayout(context, attrs, defStyle) {
    private var titleBarLeftIcon: ImageView? = null
    private var titleBarRightIcon: ImageView? = null
    private var titleBarLeftText: TextView? = null
    private var titleBarRightText: TextView? = null
    private var titleBarRight: LinearLayout? = null

    init {
        setBackgroundColor(ContextCompat.getColor(context, R.color.title_background))
        LayoutInflater.from(context).inflate(R.layout.layout_title_bar, this, true)
        titleBarLeftIcon = this.findViewById(R.id.title_bar_left_icon)
        titleBarLeftText = this.findViewById(R.id.title_bar_left_text)
        titleBarRightText = this.findViewById(R.id.title_bar_right_text)
        titleBarRightIcon = this.findViewById(R.id.title_bar_right_icon)
        titleBarRight = this.findViewById(R.id.title_bar_right)
    }

    fun getRightText(): String {
        return titleBarRightText?.text.toString()
    }

    fun getLeftText(): String {
        return this.findViewById<TextView>(R.id.title_bar_left_text).text.toString()
    }

    fun setTitleBarText(title: String?) {
        this.findViewById<TextView>(R.id.title_bar_title).text = title
    }

    fun setTitleBarColor(id: Int) =
        this.findViewById<TextView>(R.id.title_bar_title).setTextColor(id)

    fun setTitleBarRightColor(id: Int) = titleBarRightText?.setTextColor(id)

    fun setLeftDrawable(id: Int) {

        titleBarLeftIcon?.visibility = View.VISIBLE
        this.findViewById<TextView>(R.id.title_bar_left_text).visibility = View.GONE //判断是否是 RTL 布局
        if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == LayoutDirection.RTL) {
            titleBarLeftIcon?.rotationY = 180f
        }
        titleBarLeftIcon?.setImageDrawable(ContextCompat.getDrawable(context, id))
    }

    fun setLeftText(leftText: String) {
        titleBarLeftText?.visibility = View.VISIBLE
        titleBarLeftIcon?.visibility = View.GONE
        titleBarLeftText?.text = leftText
    }

    fun setLeftTextColor(colorId: Int) =
        titleBarLeftText?.setTextColor(ContextCompat.getColor(context, colorId))

    fun setRightTextColor(colorId: Int) =
        titleBarRightText?.setTextColor(ContextCompat.getColor(context, colorId))

    fun setLeftTextSize(size: Float) {
        titleBarLeftText?.textScaleX = size
    }

    fun setRightTextSize(size: Float) {
        titleBarRightText?.textSize = size
    }

    fun setRightDrawable(id: Int) {
        titleBarRight?.visibility = View.VISIBLE
        titleBarRightIcon?.visibility = View.VISIBLE
        titleBarRightText?.visibility = View.GONE
        titleBarRightIcon?.setImageDrawable(ContextCompat.getDrawable(context, id))
    }

    fun setRightText(leftText: String) {
        titleBarRight?.visibility = View.VISIBLE
        titleBarRightText?.visibility = View.VISIBLE
        titleBarRightIcon?.visibility = View.GONE
        titleBarRightText?.text = leftText
    }

    fun setLeftVisibility(visibility: Int) {
        titleBarRight?.visibility = visibility
    }

    fun setRightVisibility(visibility: Int) = toggleRight(visibility == View.VISIBLE)

    private fun toggleRight(state: Boolean) {
        if (state) {
            titleBarRight?.visibility = View.VISIBLE
        } else {
            titleBarRight?.postDelayed({ titleBarRight?.visibility = View.INVISIBLE }, 300)
        }
    }

    fun setTitleBarLeftClick(func: (() -> Unit)?) =
        this.findViewById<LinearLayout>(R.id.title_bar_left).setOnClickListener { func?.invoke() }

    fun setTitleBarTitleClick(func: () -> Unit) =
        this.findViewById<TextView>(R.id.title_bar_title).setOnClickListener { func.invoke() }

    fun setTitleBarRightClick(func: (() -> Unit)?) =
        titleBarRight?.setOnClickListener { func?.invoke() }

    fun setLeftTextAndIcon(leftText: String, iconId: Int, iconAtLeft: Boolean) {
        titleBarLeftText?.text = leftText
        titleBarLeftText?.visibility = View.VISIBLE
        titleBarLeftIcon?.visibility = View.GONE
        val drawable = ContextCompat.getDrawable(context, iconId)
        drawable?.setBounds(0, 0, dp2px(18), dp2px(18))
        if (iconAtLeft) {
            titleBarLeftText?.setCompoundDrawablesRelative(drawable, null, null, null)
        } else {
            titleBarLeftText?.setCompoundDrawablesRelative(null, null, drawable, null)
        }
        titleBarLeftText?.compoundDrawablePadding = dp2px(5)
    }

    fun setRightTextAndIcon(rightText: String, iconId: Int, iconAtLeft: Boolean) {
        titleBarRightText?.text = rightText
        titleBarRightText?.visibility = View.VISIBLE
        titleBarRightIcon?.visibility = View.GONE
        val drawable = ContextCompat.getDrawable(context, iconId)
        drawable?.setBounds(0, 0, dp2px(18), dp2px(18))
        if (iconAtLeft) {
            titleBarRightText?.setCompoundDrawablesRelative(drawable, null, null, null)
        } else {
            titleBarRightText?.setCompoundDrawablesRelative(null, null, drawable, null)
        }
        titleBarRightText?.compoundDrawablePadding = dp2px(5)
    }

    fun hideDivider() { //        title_bar_divider.visibility = View.GONE
    }

    fun useElevation(v: Float = 1f) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            elevation = dp2px(v.toInt()).toFloat()
            hideDivider()
        }
    }
}